import {
  ClassSerializerContextOptions,
  ClassSerializerInterceptor,
  PlainLiteralObject,
  StreamableFile,
} from '@nestjs/common';
import { isArray, isObject } from 'lodash';

export class AppInterceptor extends ClassSerializerInterceptor {
  serialize(
    response: PlainLiteralObject | Array<PlainLiteralObject>,
    options: ClassSerializerContextOptions,
  ): PlainLiteralObject | Array<PlainLiteralObject> {
    if (
      (!isObject(response) && !isArray(response)) ||
      response instanceof StreamableFile
    ) {
      return response;
    }
    // 如果是数组
    if (isArray(response)) {
      return (response as PlainLiteralObject[]).map((item) =>
        isObject(item) ? this.transformToPlain(item, options) : item,
      );
    }
    return this.transformToPlain(response, options);
  }
}
